# -*- coding: utf-8 -*-

from odoo import models, fields, api, _


class CpqQuickQuoteWizard(models.TransientModel):
    _name = 'cpq.quick.quote.wizard'
    _description = '快速报价向导'
    
    # ============ 基础信息 ============
    partner_id = fields.Many2one(
        'res.partner',
        string='客户',
        required=True
    )
    
    end_customer = fields.Char(
        string='最终客户',
        required=True,
        help='例如: 奔驰、宝马'
    )
    
    vehicle_model = fields.Char(
        string='车型',
        help='例如: E级W214'
    )
    
    product_tmpl_id = fields.Many2one(
        'product.template',
        string='产品',
        required=True,
        domain=[('is_configurable', '=', True)]
    )
    
    quantity = fields.Float(
        string='预计总产量',
        required=True,
        default=100000.0
    )
    
    target_price = fields.Monetary(
        string='目标价格',
        currency_field='currency_id'
    )
    
    currency_id = fields.Many2one(
        'res.currency',
        default=lambda self: self.env.company.currency_id
    )
    
    # ============ 快速配置(保险杠专用) ============
    material_type = fields.Selection([
        ('pp', 'PP增强'),
        ('ppe', 'PPE工程塑料'),
        ('pc_abs', 'PC+ABS')
    ], string='材料类型', required=True, default='pp')
    
    surface_finish = fields.Selection([
        ('plain', '素色'),
        ('metallic', '金属漆'),
        ('pearl', '珠光漆')
    ], string='表面处理', required=True, default='metallic')
    
    radar_holes = fields.Selection([
        ('0', '无'),
        ('4', '4个'),
        ('6', '6个')
    ], string='雷达孔位', default='0')
    
    sensor_integration = fields.Selection([
        ('none', '无'),
        ('camera', '摄像头'),
        ('camera_radar', '摄像头+雷达')
    ], string='传感器集成', default='none')
    
    # ============ 业务方法 ============
    def action_create_quotation(self):
        """一键创建报价"""
        self.ensure_one()
        
        # 创建简单配置
        config = self.env['cpq.configuration'].create({
            'name': f'{self.product_tmpl_id.name} - {self.partner_id.name}',
            'product_tmpl_id': self.product_tmpl_id.id,
            'state': 'draft'
        })
        
        # 根据快速选项创建配置项
        # TODO: 这里需要根据产品的属性来创建配置项
        
        # 创建报价单
        quotation = self.env['cpq.quotation'].create({
            'name': f'{self.end_customer} - {self.vehicle_model} - {self.product_tmpl_id.name}',
            'partner_id': self.partner_id.id,
            'end_customer': self.end_customer,
            'vehicle_model': self.vehicle_model,
            'product_tmpl_id': self.product_tmpl_id.id,
            'configuration_id': config.id,
            'quantity': self.quantity,
            'unit_price': self.target_price or 0.0
        })
        
        # 自动核算成本
        try:
            quotation.action_calculate_cost()
        except Exception as e:
            # 如果成本核算失败，仍然创建报价单
            pass
        
        return {
            'type': 'ir.actions.act_window',
            'name': '报价单',
            'res_model': 'cpq.quotation',
            'res_id': quotation.id,
            'view_mode': 'form',
            'target': 'current'
        }

